<?php
class ProjectsController extends AppController{

    var $name='Projects';
    var $uses = array("Attachment","Project","Codetype","ProCvalue","Group","Followinfo","Constant","Informationfrom","Competence");
    var $helpers=array('Pagination','Html','Javascript','Form','Fck','Str','Action','Accp');
    var $components = array ('Pagination');
    var $layout='hindex';
    
    //首页内容
    function index(){
    	$grouplist=$this->Group->getGroupList();
        App::import('Helper', 'Action');
        $action = new ActionHelper();
        $user=$this->Session->read('User');
        //var_dump(3);die;
        $flag = $action->checkPermissions($user,"add");
        if(!$flag){
            $this->redirect("/companys/index");
            exit();
        }
        $this->Session->delete('projectid');
        $industrys=$this->Codetype->getCValues($this->Constant->Industry,"all");
        $this->set(compact('industrys'));
        
        $pagesize=10;
        $paging['show']=$pagesize;
        $paging['url'] = '/projects/index/';
        $urlcount=0;
        
        $where="";
        $keyword="";
        $industry="";
        if(!empty($_GET['industry'])){
            $industry=$_GET['industry'];
            $objpidlist=$this->ProCvalue->findAllByCvid($industry);
            $pidlist="";
            if($objpidlist){
                foreach ($objpidlist as $pid){
                    $pidlist.="'".$pid["ProCvalue"]["pid"]."',";
                }
            }
            $wheres=rtrim($pidlist,",");
            if(empty($wheres)){
                $wheres=1;
            }
            $where="Project.id in (".$wheres.") and ";
        }
        if(!empty($_GET['first_owner'])){
            $first_owner=$_GET['first_owner'];
            $where.="(Project.first_owner = '".$first_owner."') and ";
        }
        $Phase=$this->Constant->Phase;
        $objphase=$this->Codetype->getSubType($Phase,"└─",null,"请选择所属阶段");
        $phase="";
        if(!empty($_GET["phase"])){
            $phase=$_GET["phase"];
        }
        $Registr="";
        $NRegistr=$this->Constant->NRegistr;
        $WRegistr=$this->Constant->WRegistr;
        $nreg = "└─".$NRegistr;
        $wreg = "└─".$WRegistr;
        foreach($objphase as $key=>$phases){
            if($key==$phase){
                if($phases=="└─".$NRegistr){
                    $Registr=$NRegistr;
                    break;
                }
                if($phases=="└─".$WRegistr){
                    $Registr=$WRegistr;
                    break;
                }
            }
        }
        $this->set(compact('objphase','phase','Registr'));
        if(!empty($_GET['phase'])){
            $phase=$_GET['phase'];
            if($Registr){
                $where.="(Project.reg_type = '".$phase."') and ";
            }else{
                $where.="(Project.phase = '".$phase."') and ";
            }
        }
        if(!empty($_GET['registr'])){
            $registr=$_GET['registr'];
            $where.="(Project.reg_decl_phase = '".$registr."') and ";
        }
		if(empty($_GET['group'])){
        	if(!$user['isadmin']){
        		if(!$user['isvisitor']){
				 $where.=" Company.groupid = '".$user['groupid']."' and ";     
        		}  		  		
        	}
        }else{
        	 $where.=" Company.groupid = '".$_GET['group']."' and ";
        }
        $group_mod_id=$_GET['group'];
        
        
        $askforstate = "";
        if(isset($_GET['askforstate'])){
            $askforstate=$_GET['askforstate'];
            $where.="(Project.is_audit = '".$askforstate."') and ";
        }
        //		else
        //		{
        //			$user = $this->Session->read("User");
        //			if(($user["isadmin"] || $user["isgroupadmin"]) && empty($_GET['seach']))
        //			{
        //				$askforstate = "2";
        //				$where.="(Project.is_audit = '".$askforstate."') and ";
        //			}
        //		}

        $deletestate = "";
        if(isset($_GET['deletestate'])){
            $deletestate=$_GET['deletestate'];
            $where.="(Project.is_deleted = '".$deletestate."') and ";
        }
        
        if(!empty($_GET['keyword'])){
            $keyword=$_GET['keyword'];
            $where.="(Project.company_name like '%".urldecode($keyword)."%'
            or Project.project_name like '%".urldecode($keyword)."%') and ";
        }
        $where.="Project.is_deleted !='1'";
        $this->Project->unbindModel(array ('hasMany' => array ('pros_cvalue','Attachment'),
            'belongsTo' => array ('phase_cvalue','reg_cvalue','reg_decl_cvalue')));
        $urlcount=$this->Project->findCount($where);
        $objusers=$this->Session->read('UserList');
        if($where!="Project.is_deleted !='1'"){
            $this->Session->setFlash('共检索出'.$urlcount.'条记录');
        }
        $paging['total'] =$urlcount;
        $showpage=0;
        $projects=array();
        if ($paging['total'] > 0) {
            list ($order, $limit, $page) = $this->Pagination->init("", $paging);
            $this->Project->unbindModel(array ('hasMany' => array ('pros_cvalue','Attachment'),
            'belongsTo' => array ('reg_cvalue','reg_decl_cvalue')));
            $projects=$this->Project->findAll($where,
            "`Project`.`id`, `Project`.`company_name`, `Project`.`project_name`,`Project`.`first_owner`,`Project`.`second_owner`,
            `phase_cvalue`.`value`,`Project`.`is_audit`,`Project`.`created`,`Project`.`is_deleted`",
            "Project.modified desc",$limit,$page);
            if($urlcount>$pagesize)
            {
                $showpage=1;
            }
        }
        $this->set(compact('group_mod_id','projects','showpage','keyword','industry','objusers','askforstate','deletestate','nreg','wreg','grouplist'));
    }
    function getphases($phases)
    {
        $i=0;
        $strphases="";
        foreach ($phases as $key=>$phase)
        {
            if($i==0){
                $strphases=$key;
                break;
            }
            $i++;
        }
        return $strphases;
    }
    function checkpname($pname)
    {
        $isflag=0;
        $objpname=array();
        $objpname=$this->Project->findCount("Project.project_name='".urldecode($pname)."' and Project.is_deleted!='1'");
        if($objpname>0){
            $isflag=1;
        }
        print($isflag);
        exit();
    }
    function checkcname($cname)
    {
        $isflag=0;
        $objpcname=array();
        $objpcname=$this->Project->findCount("Project.company_name='".urldecode($cname)."' and Project.is_deleted!='1'");
        $this->loadModel("Company");
        $objcname=$this->Company->findCount("Company.company_name='".urldecode($cname)."' and Company.is_deleted='0'");
        if($objpcname>0){
            $isflag=1;
            print($isflag);
            exit();
        }
        if ($objcname>0) {
            $isflag=1;
            print($isflag);
            exit();
        }
        print($isflag);
        exit();
    }

    function checkusermail($obj,$accp){
        $mail=$obj["Project"]["email"];
        $umail=$obj["User"]["uemail"];
        $flag=true;
        if($umail!=""){
            if($mail!=$umail){
                $flag=true;
            }else{
                $flag=false;
            }
        }

        if($flag){
            $email =UrlDecode($mail);
            $data=array("checkemail"=>$email);
            $objusers=$accp->accpactionuser($data);
            if($objusers["isflag"]=="1"){
                $this->Session->setFlash('邮箱已经注册过','default',array('class' => 'info_error_alert'));
                $this->redirect($this->referer());
                exit();
            }
        }
    }
	

    function checkonlymail($nowmail,$cid){
    	$flag = true;
    	$emails = $this->Project->findAll("Project.is_deleted = '0'","email");
    	if($cid){
    		$oldemail = $this->Project->find("Project.is_deleted = '0' and Project.id='".$cid."'","email");
    		if($nowmail != $oldemail['Project']['email']){
	    		foreach ($emails as $email){
	    			if($nowmail == $email['Project']['email']){
	    				$flag = false;
	    				break;
	    			}
	    		}
	    	}
    	}else{
    		foreach ($emails as $email){
    			if($nowmail == $email['Project']['email']){
    				$flag = false;
    				break;
    			}
    		}
    	}
    	
    	if(!$flag){
	    	$this->Session->setFlash('邮箱已经注册过','default',array('class' => 'info_error_alert'));
	        $this->redirect($this->referer());
	        exit();
    	}
    }
    
    function editinfo($id=null)
    {	
        $educations=$this->Constant->Education;
        unset($educations[1]);
        $this->set(compact('educations'));
        $this->Session->write('projectid',$id);
        $industrys=$this->Codetype->getCValues($this->Constant->Industry,1);
        $informations = $this->Codetype->getCValues($this->Constant->InformationFrom,1);
        $phases=$this->Codetype->getCValues($this->Constant->Phase,1);
        $Registr=$this->Constant->Registr;
        $NRegistr=$this->Constant->NRegistr;
        $WRegistr=$this->Constant->WRegistr;
        $Declare=$this->Constant->Declare;
        $Intention=$this->Constant->Intention;
        $Talk=$this->Constant->Talk;
        $this->set(compact('Registr','NRegistr','WRegistr','Declare','Intention','Talk'));
        $reg_type=$this->Codetype->getCValues($Registr);
        $nreg_phase=$this->Codetype->getCValues($NRegistr);
        $wreg_phase=$this->Codetype->getCValues($WRegistr);
        $decl_phase=$this->Codetype->getCValues($Declare);
        
        $this->set(compact('industrys','informations','reg_type','nreg_phase','wreg_phase','decl_phase'));

        $objuser=$this->Session->read('User');
        $userid=$objuser['id'];
        if(!empty($this->data))
        {
            App::import('Helper', 'Accp');
            $accp = new AccpHelper();
            $this->checkusermail($this->data,$accp);
            $this->checkonlymail($this->data['Project']['email'],$id);
            $birthday=$this->data["Project"]["birthday"];
            if(empty($birthday)){
                $this->data["Project"]["birthday"]=null;
            }

            if(empty($this->data["Project"]["reg_time"])){
                $this->data["Project"]["reg_time"]=null;
            }
            if(empty($this->data["Project"]["capital"])){
                $this->data["Project"]["capital"]=0;
            }
            if($id){
                $reg_decl_phase="";
                $txtphase=$this->data["Project"]["txtphase"];
                if($txtphase==$Registr){
                    $txtreg_type=$this->data["Project"]["txtreg_type"];
                    if($txtreg_type==$NRegistr){
                        $reg_decl_phase=$this->data["Project"]["nreg_phase"];
                    }else if($txtreg_type==$WRegistr){
                        $reg_decl_phase=$this->data["Project"]["wreg_phase"];
                    }
                }
                else if($txtphase==$Declare){
                    $this->data["Project"]["reg_type"]="";
                    $reg_decl_phase=$this->data["Project"]["decl_phase"];
                }else{
                    $this->data["Project"]["reg_type"]="";
                    $reg_decl_phase="";
                }
                $this->data["Project"]["reg_decl_phase"]=$reg_decl_phase;
            }else{
                $str=$this->getphases($phases);
                $this->data["Project"]["phase"]=$str;
                $this->data["Project"]["first_owner"] = $userid;
            }
            $this->Project->save($this->data);
            $pid=$this->data["Project"]["id"];
            if(empty($pid)){
                $pid=$this->Project->getLastInsertId();
            }else{
                $this->ProCvalue->deleteAll("pid='".$pid."'");
                $this->Informationfrom->deleteAll("pid='".$pid."'");
            }
            $this->Session->setFlash('项目引进信息保存成功');
                
            $industryids=$this->data["Project"]["industryid"];
            if($industryids){
                $objindustrys=explode(",",$industryids);
                $objindustrys=array_unique($objindustrys);
                $proscvalue=array();
                foreach($objindustrys as $industry){
                    if($industry){
                        $proscvalue["ProCvalue"]["pid"]=$pid;
                        $proscvalue["ProCvalue"]["cvid"]=$industry;
                        $this->ProCvalue->save($proscvalue);
                        $this->ProCvalue->id=null;
                    }
                }
            }
                
            $informationfromids=$this->data["Project"]["informationfromid"];
            if($informationfromids){
                $objinformationfroms=explode(",",$informationfromids);
                $objinformationfroms=array_unique($objinformationfroms);
                $info = array();
                foreach($objinformationfroms as $information){
                    if($information){
                        $info["Informationfrom"]["pid"]=$pid;
                        $info["Informationfrom"]["cvid"]=$information;
                        $this->Informationfrom->save($info);
                        $this->Informationfrom->id=null;
                    }
                }
            }
                
            $this->Session->write('projectid',$pid);
            if($this->data["Project"]["isfile"]){
                $this->redirect(array('action' => 'editfile'));exit();
            }else{
                $this->redirect(array('action' => 'index?seach=all'));exit();
            }
        }
        $pros_cvalues=array();
        $informationfroms = array();
        if($id)
        {
            $objproject=$this->Project->findById($id);
            $project_phase = $objproject["phase_cvalue"]["value"];
            $Incubate = $this->Constant->Incubate;
                
            if($project_phase != $Incubate)  array_pop($phases);

            $this->data=$objproject;
            $pros_cvalues=$objproject["pros_cvalue"];
            $informationfroms = $objproject["Informationfrom"];
            $reg_time=$this->data["Project"]["reg_time"];
            if($reg_time){
                $this->data["Project"]["reg_time"]=date("Y-m-d",strtotime($reg_time));
            }
        }
        else{
            $this->data["Project"]["cap_type"]="0";
            $this->data["Project"]["sex"]="0";
        }
        $this->set(compact('pros_cvalues','informationfroms','id','phases'));
    }

    function savepermission($permissions){
        $pid=$this->Session->read('projectid');
        $p = rtrim($permissions,",");
        $pe = explode(",",$p);
        $pes=array_unique($pe);
        $objCompetence=array();
        if($pes)
        {
            $this->Competence->deleteAll("pid='".$pid."'");
        }
        foreach($pes as $pe)
        {
            $objCompetence["Competence"]["pid"] = $pid;
            $objCompetence["Competence"]["visible"] = $pe;
            $this->Competence->save($objCompetence);
            $this->Competence->id=null;
        }
        $this->Session->setFlash('权限设置成功');
        $this->redirect(array('action' => 'editfile'));
    }

    function editfile($flag=null)
    {
        $pfiletype=$this->Codetype->getCValues("项目支撑资料",1);
        $files=$this->Constant->File;
        $this->set(compact('pfiletype'));
        $pid=$this->Session->read('projectid');
        $this->loadModel("Competence");
        $objcomptence=array();
        $a=$this->data["Competence"]["file"];
        $f=$this->data["Competence"]["value"];
        $where="";
        $where="Competence.pid='".$pid."'";
        $pidlist = $this->Competence->findAll($where);
        if(empty($pidlist)){
            foreach($pfiletype as $key=>$filetype)
            {
                if(!(in_array($filetype, $files))){
                    $objcomptence["Competence"]["pid"] = $pid;
                    $objcomptence["Competence"]["visible"] =$key;
                    $this->Competence->save($objcomptence);
                    $this->Competence->id=null;
                }
            }
        }
        if(!empty($f)){
            $this->savepermission($a);
        }
        
        if(!empty($pid) && empty($flag)){
            $this->Project->unbindModel(array ('hasMany' => array ('pros_cvalue'),
            'belongsTo' => array ('phase_cvalue','reg_cvalue','reg_decl_cvalue')));
            $objprojects=$this->Project->findAll("id='$pid'","id,project_name,first_owner");
            $objproject=$objprojects[0];
            $pname=$objproject['Project']['project_name'];
            $first_ownerid=$objproject['Project']['first_owner'];
            $attachments=$objproject["Attachment"];
            $competences=$objproject["Competence"];
            //debug($competences);
                
            $this->set(compact('attachments','pid','pname','first_ownerid','competences'));
        }else{
            App::import('Helper', 'Action');
            $actionf = new ActionHelper();
            foreach($pfiletype as $key=>$filetype){
                if(!empty($_FILES['upload'.$key])){
                    $uploads=$_FILES['upload'.$key];
                    $name=$_FILES['upload'.$key]['name'];
                    $dirpath="upload/files/";
                    for($i=0;$i<count($name);$i++)
                    {
                        if($uploads["name"][$i]){
                            $file_path=$actionf->upload_file($uploads,$dirpath,$i);
                            $objAttachment["Attachment"]["pid"] = $pid;
                            $objAttachment["Attachment"]["cvid"] = $key;
                            $objAttachment["Attachment"]["file_name"] = $uploads["name"][$i];
                            $objAttachment["Attachment"]["file_path"] = $dirpath.$file_path;
                            $this->Attachment->save($objAttachment);
                            $this->Attachment->id=null;
                        }
                    }
                }
            }
            if(!empty($this->data))
            {
                $this->Session->setFlash('附件添加成功');
                $this->redirect(array('action' => 'editfile'));
            }
        }
    }
    function delfile($id){
        $this->Attachment->id=$id;
        $isflag=$this->Attachment->saveField("is_deleted","1");
        print_r($isflag);
        exit();
    }
    //下载文件
    function download($id)
    {
        $attachments=$this->Attachment->findById($id);
        $file_title=$attachments["Attachment"]["file_name"];
        $path=$attachments["Attachment"]["file_path"];
        App::import('Helper', 'Action');
        $actionf = new ActionHelper();
        $actionf->downloads($file_title,$path);
        exit();
    }
    function moveinfo($id){

        $this->Project->unbindModel(array ('hasMany' => array ('pros_cvalue'),
        'belongsTo' => array ('phase_cvalue','reg_cvalue','reg_decl_cvalue')));
        $objproject=$this->Project->findById($id);
        $attachments=$objproject["Attachment"];
        $this->set(compact('attachments'));
        $this->showinfo($id);
    }
    function showinfo($id)
    {	
        $educations=$this->Constant->Education;
        unset($educations[1]);
        $this->set(compact('educations'));
        $this->Session->write('projectid',$id);
        $objusers=$this->Session->read('UserList');
        //$this->loadModel('User');
        $industrys=$this->Codetype->getCValues($this->Constant->Industry,1);
        $informations = $this->Codetype->getCValues($this->Constant->InformationFrom,1);
        $phases=$this->Codetype->getCValues($this->Constant->Phase,1);
        $Registr=$this->Constant->Registr;
        $NRegistr=$this->Constant->NRegistr;
        $WRegistr=$this->Constant->WRegistr;
        $Declare=$this->Constant->Declare;
        $Incubate = $this->Constant->Incubate;
        $Intention=$this->Constant->Intention;
        $Talk=$this->Constant->Talk;
        $this->set(compact('Registr','NRegistr','WRegistr','Declare','Incubate','Intention','Talk','objusers'));

        $reg_type=$this->Codetype->getCValues($Registr,1);
        $nreg_phase=$this->Codetype->getCValues($NRegistr,1);
        $wreg_phase=$this->Codetype->getCValues($WRegistr,1);
        $decl_phase=$this->Codetype->getCValues($Declare,1);
        $this->set(compact('industrys','informations','phases','reg_type','nreg_phase','wreg_phase','decl_phase'));

        $pros_cvalues=array();
        $objproject=array();
        $informationfroms = array();
        $competences=array();
        $attachments = array();
        if($id)
        {
            $objproject=$this->Project->findById($id);
                
            $this->data=$objproject;
            $pros_cvalues=$objproject["pros_cvalue"];
            $informationfroms = $objproject["Informationfrom"];
            $competences=$objproject["Competence"];
            $is_audit = $objproject["Project"]["is_audit"];
            $unaudit_reason = $objproject["Project"]["unaudit_reason"];
            $undelete_reason = $objproject["Project"]["undelete_reason"];
            //			debug($limits);
            //如果移交的状态为申请中或者已移交，那么就attachements中存放已提交的附件而不是所有的附近
            if($is_audit == "1" || $is_audit == "2")
            {
                $i = 0;
                foreach($objproject["Attachment"] as $attachment)
                {
                    if($attachment["is_submit"] == "1")
                    {
                        $attachments[$i] = $attachment;
                        $i++;
                    }
                }
            }
            else
            $attachments=$objproject["Attachment"];
            $reg_time=$this->data["Project"]["reg_time"];
            $is_deleted = $objproject["Project"]["is_deleted"];
            if($reg_time){
                $this->data["Project"]["reg_time"]=date("Y-m-d",strtotime($reg_time));
            }
        }

        $this->set(compact('pros_cvalues','informationfroms','is_deleted','objproject','id','attachments','unaudit_reason','undelete_reason','competences'));
    }
    function changesave($id,$column,$content)
    {
        $this->Project->id=$id;
        $this->Project->saveField($column,urldecode($content));
        print(1);
        exit();
    }

    //	//改变项目的申请状态为申请中
    //	function editIsauditAskfor($id)
    //	{
    //		$this->Project->id=$id;
    //		$this->Project->saveField("is_audit","2");
    //		App::import('Helper', 'Accp');
    //		$accp = new AccpHelper();
    //		$data=array("guid"=>$id,"event"=>"askForTurnOver","usertype"=>"1");
    //		$isflag=$accp->accpalert($data);
    //
    //		$objids=$this->data["fileidlist"];
    //		if($objids)
    //		{
    //			foreach ($objids as $fid)
    //			{
    //				$objfile=$this->Attachment->findAll("Attachment.id='".$fid."' and Attachment.is_deleted='0'");
    //			}
    //		}
    //		debug($objids);
    //		//debug($objfile);
    //
    //		$this->Session->setFlash('项目已申请移交');
    //		//$this->redirect("/projects/index");
    //		exit();
    //	}
    //
    //	//改变项目的申请状态为驳回
    //	function editIsauditReject($id)
    //	{
    //		$this->Project->id=$id;
    //		$this->Project->saveField("is_audit","3");
    //		$this->Session->setFlash('项目已驳回移交');
    //		$this->redirect("/projects/index");
    //		exit();
    //	}

    //改变项目的删除状态为申请中
    function editIsdeleted($id)
    {
        $this->Project->id=$id;
        $this->Project->saveField("is_deleted","2");
        App::import('Helper', 'Accp');
        $accp = new AccpHelper();
        $data=array("guid"=>$id,"event"=>"askForDelete","usertype"=>"1");
        $isflag=$accp->accpalert($data);
        $this->Session->setFlash('项目已申请删除');
        $this->redirect("/projects/index");
        exit();
    }

    //	//改变项目的删除状态为驳回
    //	function editIsdeletedReject($id)
    //	{
    //		$this->Project->id=$id;
    //		$this->Project->saveField("is_deleted","3");
    //		$this->Session->setFlash('项目已驳回删除');
    //		$this->redirect("/projects/index");
    //		exit();
    //	}

    function moveproject($id)
    {
        if($this->data["companys"]["rejectDelete"] == "2")
        {
            $this->Project->id=$id;
            $this->Project->saveField("is_deleted","3");
            $this->Session->setFlash('项目已驳回删除');
            $this->redirect("/projects/index");
            exit();
        }
        //点击了申请提交按钮后的操作
        if($this->data["companys"]["editIsauditAskFor"] == "1")
        {
            $this->Project->id=$id;
            $this->Project->saveField("is_audit","2");
            App::import('Helper', 'Accp');
            $accp = new AccpHelper();
            $data=array("guid"=>$id,"event"=>"askForTurnOver","usertype"=>"1");
            $isflag=$accp->accpalert($data);
                
            if(!empty($this->data["fileidlist"]))
            $objids=$this->data["fileidlist"];
            if(!empty($objids))
            {
                foreach ($objids as $fid)
                {
                    $this->Attachment->id = $fid;
                    $this->Attachment->saveField("is_submit","1");
                }
            }
            $this->Session->setFlash('项目已申请移交');
            $this->redirect("/projects/index");
        }
        //点击驳回移交时的操作
        else if($this->data["companys"]["editIsauditAskFor"] == "2")
        {
            $this->Project->id=$id;
            $this->Project->saveField("is_audit","3");
                
            if(!empty($this->data["fileidlist"]))
            $objids=$this->data["fileidlist"];
            if(!empty($objids))
            {
                foreach ($objids as $fid)
                {
                    $this->Attachment->id = $fid;
                    $this->Attachment->saveField("is_submit","0");
                }
            }
                
            $this->Session->setFlash('项目已驳回移交');
            $this->redirect("/projects/index");
        }
        //点击了确定移交按钮后的操作
        else
        {
            $this->loadModel('Company');
            $companycount=$this->Company->findCount("Company.pid='$id' and Company.is_deleted='0'");
            if($companycount>0){
                $this->Session->setFlash('项目已经移交');
                $this->redirect("/companys/index");
                exit();
            }
            $this->Project->unbindModel(array ('hasMany' => array ('pros_cvalue','Attachment'),
            'belongsTo' => array ('reg_cvalue','reg_decl_cvalue','phase_cvalue')));
            $this->Project->id=$id;
            $this->Project->saveField('is_audit','1');
            $objproject=$this->Project->findById($id);
            $objp=$objproject["Project"];
            $objcompanys=array();
            foreach ($objp as $key=>$project)
            {
                if($key!="id" && $key!="is_deleted" && $key!="created" && $key!="modified"){
                    $objcompanys["Company"][$key]=$project;
                    if($key=="reg_time"){
                        if($project==""){
                            $objcompanys["Company"]["reg_time"]=null;
                        }
                    }
                    if($key=="birthday"){
                        if($project==""){
                            $objcompanys["Company"]["birthday"]=null;
                        }
                    }
                }
            }
            $objcompanys["Company"]["pid"]=$id;
            $objcompany=$this->Company->save($objcompanys);
            $contact=$objcompany["Company"]["contact"];
            $cid=$this->Company->getLastInsertId();
            $email=$objcompany["Company"]["email"];
            $tel=$objcompany["Company"]["tel"];
            $groupid=$this->Constant->CompanyGroupId;
            $this->loadModel("User");
            $this->User->addcompanyuser($cid,$contact,$email,"",$tel,null);
            App::import('Helper', 'Action');
            $path=Router::url(array('controller' => '/', 'action' =>''), true);
            $actionf = new ActionHelper();
            $actionf->ybsend($path."/phpmailer/testemail.php?title=".base64_encode($contact)."&email=".base64_encode($email));//发送邮件
//            App::import('Helper', 'Accp');
//            $accp = new AccpHelper();
//            $data=array("name"=>$contact,"email"=>$email,"mobile"=>$tel,
//            "cid"=>$cid,"usertype"=>"2","groupid"=>$groupid);
//            $regdata=$accp->accpreg($data);
//            $data=array("guid"=>$cid,"event"=>"addcompany","usertype"=>"1");
//            $isflag=$accp->accpalert($data);
                
            $objindustrys=$objproject["pros_cvalue"];
            foreach($objindustrys as $industry){
                if($industry["cvid"]){
                    $proscvalue["ProCvalue"]["pid"]=$cid;
                    $proscvalue["ProCvalue"]["cvid"]=$industry["cvid"];
                    $this->ProCvalue->save($proscvalue);
                    $this->ProCvalue->id=null;
                }
            }
            //移交附件
            $objids = "";
            if(!empty($this->data["fileidlist"]))
            $objids=$this->data["fileidlist"];
            if($objids){
                $objfiles=array();
                foreach ($objids as $fid){
                    if($fid!=0){
                        $this->Attachment->unbindModel(array ('belongsTo' => array ('filetype')));
                        //$objfile=$this->Attachment->findAll("Attachment.id='".$fid."' and Attachment.is_deleted='0'");
                        $this->Attachment->id = $fid;
                        $this->Attachment->saveField("is_submit","1");
                        $objfile = $this->Attachment->find('first',array('conditions'=>array('Attachment.id'=>$fid,'Attachment.is_deleted'=>'0')));
                        $ofile=$objfile['Attachment'];
                        $arrfile=array("pid"=>$cid,
                        "file_name"=>$ofile["file_name"],"file_path"=>$ofile["file_path"]);
                        $objfiles[]=$arrfile;
                    }
                }
                if($objfiles){
                    $this->Attachment->saveAll($objfiles);
                }
            }

            $phases=$this->Codetype->getCValues($this->Constant->Phase,1);
            $Incubate = $this->Constant->Incubate;
            foreach($phases as $key=>$phase)
            {
                if($phase == $Incubate)
                $now_phase = $key;
            }
            $this->Project->saveField("phase",$now_phase);
                
            $this->Session->setFlash('项目信息移交成功');
            $this->redirect("/companys/index");
        }
    }
    function editphase($pid,$phase)
    {
        $this->Project->id=$pid;
        $isedit=$this->Project->saveField('phase', $phase);
        print_r($isedit);
        exit();
    }
    function editregtype($pid,$regtype)
    {
        $this->Project->id=$pid;
        $isedit=$this->Project->saveField('reg_type', $regtype);
        print_r($isedit);
        exit();
    }
    function editsubphase($pid,$subphase)
    {
        $this->Project->id=$pid;
        $isedit=$this->Project->saveField('reg_decl_phase', $subphase);
        print_r($isedit);
        exit();
    }
    function editremark($pid,$remark)
    {
        $this->Project->id=$pid;
        $isedit=$this->Project->saveField('remark', urldecode($remark));
        print_r(urldecode($remark));
        exit();
    }
    function dels($id)
    {
        $this->Project->id=$id;
        $this->Project->saveField("is_deleted","1");
        $this->Session->setFlash('项目引进信息删除成功');
        $this->redirect(array('action' => 'index?seach=all'));
    }
    //项目活动
    //首页内容
    function followinfos()
    {
        $pid=$this->Session->read('projectid');
        $this->showfilefollow($pid);
        $pagesize=10;
        $paging['show']=$pagesize;
        $paging['url'] = '/projects/followinfos/';
        $urlcount=0;
        $where="";
        $where.="(Followinfo.is_deleted='0' and Followinfo.pid ='".$pid." ')";
        $this->Followinfo->unbindModel(array ('belongsTo' => array ('cvid')));
        $urlcount=$this->Followinfo->findCount($where);
        $paging['total'] =$urlcount;
        $showpage=0;
        $followinfos=array();
        if ($paging['total'] > 0) {
            list ($order, $limit, $page) = $this->Pagination->init("", $paging);
            $this->Followinfo->unbindModel(array ('belongsTo' => array ('cvid')));
            $followinfos=$this->Followinfo->findAll($where,
            "`Followinfo`.`id`, `Followinfo`.`pid`,  `Followinfo`.`owner`,`Followinfo`.`content`,
            `Followinfo`.`ctime`,`Followinfo`.`cvid`,`Followinfo`.`myperson`,`Followinfo`.`yourperson`,`Followinfo`.`remark`,`Followinfo`.`created`,`Followinfo`.`is_deleted`",
            "Followinfo.modified desc",$limit,$page);
            if($urlcount>$pagesize)
            {
                $showpage=1;
            }
        }
        $this->set(compact('followinfos','showpage'));
    }

    function showfollowinfo($id) {
        $this->editfollowinfo($id);
    }
    function addfollowinfo() {
        $pid=$this->Session->read('projectid');
        $this->showfilefollow($pid);
        $this->set('active',$this->Codetype->getCValues("活动类型"));
        if (!empty($this->data)) {
            if ($this->Followinfo->save($this->data)) {
                $this->Session->setFlash('项目活动添加成功');
                $this->redirect('/projects/followinfos/');
            }
        }
    }
    function editfollowinfo($id = null){
        $report_process=$this->Codetype->getCValues($this->Constant->Phase);
        $this->set('active',$this->Codetype->getCValues($this->Constant->Active));
        $this->set(compact('id','report_process'));
        $pid=$this->Session->read('projectid');
        $objproject=$this->Project->findById($pid);
        $this->showfilefollow($pid);
        if($id){
            $objf=$this->Followinfo->findById($id);
            $this->data=$objf;
            $this->data["Followinfo"]["ctime"]=date("Y-m-d",strtotime($objf["Followinfo"]["ctime"]));

        }else{
            if (empty($this->data)) {
                $this->data["Followinfo"]["pro"]=0;
                $this->Followinfo->unbindModel(array ('belongsTo' => array ('cvid')));
                $this->data["Followinfo"]["report_process"]=$objproject["Project"]["phase"];
            } else {
                if ($this->Followinfo->save($this->data)) {
                    $this->Session->setFlash('保存成功');
                    $this->redirect('/projects/followinfos/');
                }
            }
        }
    }
    function delfollowinfo($id){
        $followinfo=$this->Followinfo->findById($id);
        $followinfo["Followinfo"]["is_deleted"]=1;
        $this->Followinfo->save($followinfo);
        $this->Session->setFlash('删除成功');
        $this->redirect('/projects/followinfos/');
    }
    function showfilefollow($pid)
    {
        $this->Project->unbindModel(array ('hasMany' => array ('pros_cvalue','Attachment'),
            'belongsTo' => array ('phase_cvalue','reg_cvalue','reg_decl_cvalue')));
        $objprojects=$this->Project->findAll("id='$pid'","id,project_name,first_owner");
        $objproject=$objprojects[0];
            
        $pname=$objproject['Project']['project_name'];
        $first_ownerid=$objproject['Project']['first_owner'];
        $this->set(compact('pid','pname','first_ownerid'));
    }
    //中文
    function chn()
    {
        $language = 'chn';
        $this->Session->write('Config.language', $language);
        $this->redirect($this->referer());
    }

    //提醒信息
    function alert()
    {

    }

    function prompt($uid){
        $this->layout=null;
        App::import('Helper', 'Accp');
        $accp = new AccpHelper();
        //$user=$this->Session->read('User');
        //$uid=$user["id"];
        $url = $accp->path."/notification/?uid=".$uid;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl,CURLOPT_POSTFIELDS,null);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        $result  = curl_exec($curl);
        curl_close($curl);
        print_r($result);
        exit();
    }


    //英文
    function eng()
    {
        $language = 'eng';
        $this->Session->write('Config.language', $language);
        $this->redirect($this->referer());
    }
    //打开页面前执行的方法
    function beforeFilter()
    {
        parent::beforeFilter();
        $user=$this->Session->read('User');
        if (!$user){
            if($this->action=="prompt"){
                echo '-';
                exit;
            }
            $this->redirect('/users/login');
        }
    }
    //打开页面后执行的方法
    function afterFilter()
    {

    }
}
?>